/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						 simple_adv_object_image.h

	$Header: /heroes4/simple_adv_object_image.h $

	$NoKeywords: $

 ************************************************************************/

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "simple_adv_object_image.h"

#include "screen_point.h"

// ------------------------------------------------------------------------------
// Unnamed namespace
// ------------------------------------------------------------------------------

namespace
{
	// ------------------------------------------------------------------------------
	// ------------------------------------------------------------------------------
	int const k_current_format_version = 1;

} // Unnamed namespace

// ------------------------------------------------------------------------------
// t_simple_adv_object_image_24 class members
// ------------------------------------------------------------------------------

// ------------------------------------------------------------------------------
// ------------------------------------------------------------------------------
void t_simple_adv_object_image_24::get_frames( t_bitmap_group_24 & bitmap_group ) const
{
	bitmap_group = m_sequence.get_frames();
}

// ------------------------------------------------------------------------------
// ------------------------------------------------------------------------------
bool t_simple_adv_object_image_24::set_frames( t_bitmap_group_24 const & bitmap_group )
{
	t_screen_rect old_rect = m_sequence.get_rect();

	if ( !m_sequence.set_frames( bitmap_group ) )
		return false;

	// Adjust the offset to trim any excess
	m_sequence.set_offset( -get_extent( m_sequence.get_rect(), m_sequence.get_shadow_rect() ).top_left() );

	// Handle any change in the rect
	t_screen_rect new_rect = m_sequence.get_rect();
	if ( new_rect != old_rect )
		on_rect_changed( old_rect, new_rect );

	return true;
}

// ------------------------------------------------------------------------------
// ------------------------------------------------------------------------------
bool t_simple_adv_object_image_24::set_frames( t_bitmap_group_24 const & bitmap_group , t_screen_point const & offset)
{
	t_screen_rect old_rect = m_sequence.get_rect();

	if ( !m_sequence.set_frames( bitmap_group ) )
		return false;

	m_sequence.set_offset(offset);

	// Handle any change in the rect
	t_screen_rect new_rect = m_sequence.get_rect();

	if ( new_rect != old_rect )
		on_rect_changed( old_rect, new_rect );

	return true;
}

// ------------------------------------------------------------------------------
// ------------------------------------------------------------------------------
bool t_simple_adv_object_image_24::read( std::streambuf & stream )
{
	int version;
	try
	{
		version = get< t_uint16 >( stream );
	}
	catch ( t_streambuf_read_failure const & )
	{
		return false;
	}

	return read_version( stream, version );
}

// ------------------------------------------------------------------------------
// ------------------------------------------------------------------------------
bool t_simple_adv_object_image_24::read_version( std::streambuf & stream, int version )
{
	t_screen_rect old_rect = m_sequence.get_rect();

	bool result;
	if ( version >= 1 )
		result = m_sequence.read( stream );
	else
		result = m_sequence.read_version( stream, 0 );
	if ( !result )
		return false;

	t_screen_rect new_rect = m_sequence.get_rect();
	if ( new_rect != old_rect )
		on_rect_changed( old_rect, new_rect );

	return true;
}

// ------------------------------------------------------------------------------
// ------------------------------------------------------------------------------
bool t_simple_adv_object_image_24::write( std::streambuf & stream ) const
{
	try
	{
		put< t_uint16 >( stream, k_current_format_version );
	}
	catch ( t_streambuf_write_failure const & )
	{
		return false;
	}

	return m_sequence.write( stream );
}

// ------------------------------------------------------------------------------
// t_simple_adv_object_image members
// ------------------------------------------------------------------------------

// ------------------------------------------------------------------------------
// ------------------------------------------------------------------------------
void t_simple_adv_object_image::draw_to(
	int						frame_num,
	t_screen_rect const &	source_rect,
	t_abstract_bitmap16 &	dest,
	t_screen_point const &	dest_point,
	int						alpha ) const
{
	m_sequence.draw_to( frame_num, source_rect, dest, dest_point, alpha );
}

// ------------------------------------------------------------------------------
// ------------------------------------------------------------------------------
void t_simple_adv_object_image::draw_to(
	int						frame_num,
	t_abstract_bitmap16 &	dest,
	t_screen_point const &	dest_point ) const
{
	m_sequence.draw_to( frame_num, dest, dest_point );
}

// ------------------------------------------------------------------------------
// ------------------------------------------------------------------------------
void t_simple_adv_object_image::draw_shadow_to(
	int						frame_num,
	t_screen_rect const &	source_rect,
	t_abstract_bitmap16 &	dest,
	t_screen_point const &	dest_point ) const
{
	m_sequence.draw_shadow_to( frame_num, source_rect, dest, dest_point );
}

// ------------------------------------------------------------------------------
// ------------------------------------------------------------------------------
void t_simple_adv_object_image::draw_shadow_to(
	int						frame_num,
	t_abstract_bitmap16 &	dest,
	t_screen_point const &	dest_point ) const
{
	m_sequence.draw_shadow_to( frame_num, dest, dest_point );
}
